package com.bruce.pattern.structural.proxy;

import com.bruce.pattern.structural.proxy.dynamicproxy.CglibDBQueryInterceptor;
import com.bruce.pattern.structural.proxy.dynamicproxy.DBQueryI;
import com.bruce.pattern.structural.proxy.dynamicproxy.DBQueryImpl;
import com.bruce.pattern.structural.proxy.dynamicproxy.JdkDBQueryHandler;
import com.bruce.pattern.structural.proxy.staticproxy.OrderServiceStaticProxy;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;

/**
 * Created by bruce on 2018/11/8 23:12
 */
@Slf4j
public class MainTest {

    static {
        System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");   //该设置用于输出jdk动态代理产生的类

        // System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\class"); //该设置用于输出cglib动态代理产生的类
    }

    @Test
    public void staticProxy() {
        Order order = new Order();
        order.setOrderInfo("衣服");
        order.setUserId(2);

        IOrderService iOrderService = new OrderServiceStaticProxy();
        iOrderService.saveOrder(order);
    }

    @Test
    public void jdkProxy1() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        long start = System.currentTimeMillis();
        DBQueryI jdkProxy2 = (DBQueryI) Proxy.newProxyInstance(classLoader,
                new Class[]{DBQueryI.class}, new JdkDBQueryHandler());

        log.info("jdk 动态代理创建时间" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            jdkProxy2.userName("xiaoming");
        }
        log.info("调用耗时" + (System.currentTimeMillis() - start));
    }


    public static void main(String[] args) {
        MainTest mainTest = new MainTest();
        mainTest.jdkProxy1();

    }




    @Test
    public void jdkProxy2() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        long start = System.currentTimeMillis();

        Class<?> proxyClass = Proxy.getProxyClass(DBQueryI.class.getClassLoader(), DBQueryI.class);
        Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
        DBQueryI dbQueryI = (DBQueryI) constructor.newInstance(new JdkDBQueryHandler());
        log.info("jdk 动态代理创建时间" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            dbQueryI.userName("xiaoming");
        }
        log.info("调用耗时" + (System.currentTimeMillis() - start));
    }

    @Test
    public void cglibProxy() {
        long start = System.currentTimeMillis();
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(new CglibDBQueryInterceptor());
        //enhancer.setInterfaces(new Class[]{DBQueryI.class});
        enhancer.setSuperclass(DBQueryImpl.class);

        DBQueryImpl dbQueryI = (DBQueryImpl) enhancer.create();
        log.info("cglib 动态代理创建时间" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            dbQueryI.userName("hahah");
        }
        log.info("调用耗时" + (System.currentTimeMillis() - start));

       // dbQueryI.helloCglib();
        //dbQueryI.helloCglib2();
    }


}
